package com.tavio.tbs.sprite;

import org.andengine.entity.modifier.AlphaModifier;
import org.andengine.entity.modifier.LoopEntityModifier;
import org.andengine.entity.modifier.SequenceEntityModifier;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.tmx.TMXTile;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.vbo.VertexBufferObjectManager;

import android.opengl.GLES20;

import com.tavio.tbs.scene.SceneManager;
import com.tavio.tbs.structure.Position;
import com.tavio.tbs.unit.Unit;
import com.tavio.tbs.world.WorldMap;

public class SelectionRectSprite extends Sprite{
	
	private Position position; 
	
	private Unit responsibleUnit;
	
	final LoopEntityModifier entityModifier =
			new LoopEntityModifier(
					new SequenceEntityModifier(
							new AlphaModifier(1, (float)0.7, (float)0.3),
							new AlphaModifier(1, (float)0.3, (float)0.7)
					)
			);
	
	public SelectionRectSprite(Position position, float width, float height, final ITextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager, Unit responsibleUnit) {
		super(0, 0, width, height, pTextureRegion,  pVertexBufferObjectManager);
		TMXTile tile = WorldMap.getInstance().getTmxLayer(0).getTMXTile(position.getX(), position.getY());
		this.setPosition(tile.getTileX() + WorldMap.getInstance().getTileWidth()/2 - this.getWidth() / 2 , tile.getTileY() + WorldMap.getInstance().getTileHeight()/2 - this.getHeight() / 2);
		this.setPosition(position);
		this.setBlendFunction(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
		this.registerEntityModifier(entityModifier);
		this.responsibleUnit = responsibleUnit;
		SceneManager.getInstance().getScene().registerTouchArea(this);
	}

	@Override
	public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
		if(pSceneTouchEvent.isActionUp())
		{
			this.responsibleUnit.move(position);
			SceneManager.getInstance().removeSelectionRects();
		}
		return true;
	}
	
	public Position getPosition() {
		return position;
	}

	public void setPosition(Position position) {
		this.position = position;
	}
	
	
}
